<?php
/**
 * 
 * @param code de l'erreur
 * @return le message $msg html 
 */
require("config.php");
function error_message($error) {
	if($error==1){
	   $msg="<div class=\"success\">utilisateur cr&eacute;e avec succ&eacute;s !</div>";	
	}
    if($error==2){
       $msg="<div class=\"warning\">Veuillez saisir un autre identifiant !</div>"; 
    }
    if($error==3){
       $msg="<div class=\"error\">Param&eacute;tres d'identification incorrect !</div>"; 
    }
    if($error==4){
       $msg="<div class=\"warning\">Veuillez vous identifier svp !</div>"; 
    }
	if($error==5){
	   $msg="<div class=\"success\">Chargement de la base effectu&eacute;e avec succ&eacute;s !</div>";	
	}
	if($error==6){
	   $msg="<div class=\"success\">Action valid&eacute;e avec succ&eacute;s !</div>";	
	}
	if($error==7){
	   $msg="<div class=\"success\">Suppression effectu&eacute;e avec succ&eacute;s !</div>";	
	}
	if($error==8){
	   $msg="<div class=\"success\">Mails envoy&eacute;s avec succ&eacute;s aux destinataires !</div>";	
	}
	if($error==9){
	   $msg="<div class=\"success\">Affectation effectu&eacute;e avecc succ&eacute;s !</div>";	
	}
	if($error==10){
		   $msg="<div class=\"success\">Un email contenant vos param&eacutres de connexion envoy&eacute;e dans votre boite ! !</div>";	
		}
	if($error==11){
		   $msg="<div class=\"warning\">Email non r&eacute;pertori&eacute; dans la base ! !</div>";	
		}
	return $msg;
}

/**
 * 
 * @param $login v&eacute;rifie si un utilisateur existe 
 */

function ifExistLogin($login) {
	$sql="select login from users where login='$login'";
	if(mysql_num_rows(mysql_query($sql))){
		return TRUE;
	}else{
		return FALSE;
	}
}

function create_distributeur($codesap,$nom,$groupe,$idzone,$idreseau) {
	 $sql="insert into distributeurs values('','$codesap','$nom','$groupe','$idzone','$idreseau','$codesap','1234')"; 
	//echo "<br>";
	mysql_query($sql);
	
	$id = mysql_insert_id();
	return $id;
}

function getIdZone($zone) {
	$sql="select id from zones where descrip like '%$zone%'";
	$res  = mysql_query($sql);
	$r = mysql_fetch_object($res);
	return $r->id;
}
function getIdReseau($reseau) {
	$sql="select id from reseau where descrip like '%$reseau%'";
	$res  = mysql_query($sql);
	$r = mysql_fetch_object($res);
	return $r->id;
}

function create_manager($desc,$email,$fonction) {
	
	$t = explode(" ",$desc);
	$nom = $t[0];
	$prenom = $t[1];
	
	$sql="insert into managers values('','$nom','$prenom','$email','$fonction')";
	mysql_query($sql) or die("pb : $sql");
	
	$id = mysql_insert_id();
	return $id;
}

function getIdManagerByEmail($email){
	$sql="select id from managers where email='$email'";
	if($res  = mysql_query($sql)){
	$r = mysql_fetch_object($res);
	}
	return $r->id;
}

function ifEmailExist($email) {
	echo $sql="select id from managers where email='$email'";echo"<br>";
	if(mysql_num_rows($res  = mysql_query($sql))>0){
		$val="ok";
	}else{
		$val="nok";
	}
	return $val;
}
function create_AssoDisMan($id_dis,$id_man,$idzones,$idres) {
	$sql="insert into asso_dis_man values ('$id_dis','$id_man','$idres','$idzones')";
	mysql_query($sql);
}

function getIdManagerByDis($id_dis) {
	$sql="select id_man from asso_dis_man where id_distrib=$id_dis";
	
	if($res  = mysql_query($sql)){
		$r = mysql_fetch_object($res);
	}
	return $r->id_man;
}
function getIdRqByDis($id_dis) {
	$sql="select id_rq from asso_dis_resp_qual where id_distrib=$id_dis";
	
	if($res  = mysql_query($sql)){
	$r = mysql_fetch_object($res);
	}
	return $r->id_rq;
}
function getManagernameById($id_man) {
	$sql="select * from managers where id=$id_man";
	
	if($res  = mysql_query($sql)){
		$r = mysql_fetch_object($res);
		
		$val ="$r->nom,$r->prenom,$r->email,$r->fonction";
	}
	return $val;
}

function getRqNameById($id_rq) {
	$sql="select * from respon_qual where id=$id_rq";
	if($res  = mysql_query($sql)){
	$r = mysql_fetch_object($res);
	
	$val ="$r->nom,$r->prenom,$r->email,$r->fonction";
	}
	return $val;
}

function getZoneNameById($id) {
	$sql="select descrip from zones where id=$id";
	if($res  = mysql_query($sql)){
	$r = mysql_fetch_object($res);
	}
	return $r->descrip;
}


function getReseauNameById($id) {
	$sql="select descrip from reseau where id=$id";
	if($res  = mysql_query($sql)){
	$r = mysql_fetch_object($res);
	}
	return $r->descrip;
}


function getStatutInterv($id_interv) {
	$statut="";
	
	 $sql="select arg_res,arg_rt,decision from data_temp where id_interv=$id_interv";
	if($res  = mysql_query($sql)){
	$r = mysql_fetch_object($res);
		 //if(($r->arg_res!=" ") and($r->decision==" ")){
		 if(!empty($r->arg_res) and empty($r->decision)){
		 	 $statut="Argumentaire en attente de validation ";
		// }elseif(($r->arg_res!=" ") and ($r->decision!=" ")){
		 }elseif(!empty($r->arg_res) and !empty($r->decision)){
		 	$statut="Argumentaire valid&eacute; ";
		 }
	}
	 $up = "update data_temp set statut='$statut' where id_interv=$id_interv";
	 mysql_query($up);
	 return $statut;
}

function getArg_rt_decision($type) {
	if($type==1){
		$dec="R&eacute;affectation sur un autre &eacute;tablissement";
	}elseif($type==2){
		$dec="Basculer la note � 8";
	}elseif($type==3){
		$dec="Neutraliser le retour";
	}elseif($type==4){
		$dec="Neutraliser le retour + Basculer la note � 8";
	}elseif($type==5){
		$dec="Refus";
	}elseif($type==6){
		$dec="Annulation interv";
	}else{
		$dec="";
	}
	return $dec;
}

function getStatutSynthEnvoi($id_interv) {
	$sql="select DATE_FORMAT(date_envoi,'%d-%m-%Y %H:%i') as date_envoi from rt_synth_mail where id_interv=$id_interv";
	 $res = mysql_query($sql);
	 $r = mysql_fetch_object($res);
	
	 return $r->date_envoi;
}

function getDateInterv($id_interv) {
	 $sql="select date_interv from data_temp where id_interv=$id_interv";
	 $res = mysql_query($sql);
	 $r = mysql_fetch_object($res);
	
	$val_d = $r->date_interv;
	$t = explode('/',$val_d);
	$ch="$t[2]-$t[1]-$t[0]";
	return $ch;
}

function getVagueEnCours() {
	$sql="select descrip from vagues_camp where statut='EN_COURS'";
	$val="novague";
        $res = @@mysql_query($sql);
        if(mysql_num_rows($res)>0){
            	
            $r = mysql_fetch_object($res);
                $val =$r->descrip;
	 }
	 return $val;
}

function isIntervInVague($id_interv) {
	$vague_en_cours = getVagueEnCours();
        $val="readonly";
        if($vague_en_cours!="novague"){
            $sql="select * from data_temp 
                    join vagues_camp on descrip = vague
            where vague like '%$vague_en_cours%' and id_interv=$id_interv";
            //echo $sql;
            $val="disabled";
            $res = @@mysql_query($sql);
            if(mysql_num_rows($res)>0){
                    $r = mysql_fetch_object($res);
                    $val=" ";

            }
        }
	return $val;
}

function getCouleurRecom($val) {
	$color="tab_dis_data2";
	if($val<6){
		$color="tab_dis_data4";	
	}elseif($val==6 or $val==7){
		$color="tab_dis_data5";	
	}elseif($val>=8 and $val<=10){
		$color="tab_dis_data3";	
	}
	return $color;
}

function getCouleurStatut($statut) {
	if($statut==0){
		$color="tab_dis_data4";	
	}
	if($statut>0){
		$color="tab_dis_data3";	
	}
	return $color;
}

function getDistribChecked($id_distrib,$id_user,$profil) {
	$val=" ";
	
	if($profil=="MAN"){
		$sql_chk="select id_distrib ,id_man from asso_dis_man where id_man=$id_user and id_distrib=$id_distrib";
		$res_chk = mysql_query($sql_chk);
		if(mysql_num_rows($res_chk)){
			$r = mysql_fetch_object($res_chk);
			$val = $r->id_distrib;
		}
	}
	if($profil=="RQ"){
	   $sql_chk="select id_distrib ,id_rq from asso_dis_resp_qual where id_rq=$id_user and id_distrib=$id_distrib";
		$res_chk = mysql_query($sql_chk);
		if(mysql_num_rows($res_chk)){
			$r = mysql_fetch_object($res_chk);
			$val = $r->id_distrib;
		}
	}
	
	if($profil=="DIS"){
			$val = $id_distrib;
	}
	
	return $val;
}


function getCodesap($id_distrib) {
	$sql="select codesap from distributeurs where id=$id_distrib";
	$res = mysql_query($sql);
	$r = mysql_fetch_object($res);
	return $r->codesap;
}

function getIdDisbyCodeSap($codesap) {
	$sql="select id from distributeurs where codesap=$codesap";
	$res = mysql_query($sql);
	$r = mysql_fetch_object($res);
	return $r->id;
}

function histo_connection($id_user,$profil) {
	 $sql="insert into histo_con(id_user,profil,date_con) values('$id_user','$profil',NOW())";
	mysql_query($sql) or die("pb : $sql");;
	
}

function getDistribName($id_distrib) {
	$sql="select nom from distributeurs where id=$id_distrib";
	$res = mysql_query($sql);
	$r = mysql_fetch_object($res);
	return $r->nom;
}

function createVague($vague) {
	$sql="select * from vagues where descrip like '%$vague%'";
	$res = mysql_query($sql);
	if(mysql_num_rows($res)){
		
	}else{
		$i = "insert into vagues values ('','$vague')";
		mysql_query($i);
	}
}



function getCodeSapByIdDis($id_dis) {
	$sql="select codesap from distributeurs where id=$id_dis";
	$res = mysql_query($sql);
	$r = mysql_fetch_object($res);
	return $r->codesap;
}


function getDecision($i) {
if($i==1) {$val="R&eacute;affectation sur un autre &eacute;tablissement";
}elseif($i==2) {$val="Basculer la note � 8";
}elseif($i==3) {$val="Neutraliser le retour";
}elseif($i==4) {$val="Neutraliser le retour + Basculer la note � 8";
}elseif($i==5) {$val="Refus";
}elseif($i==6) {$val="Annulation interv";
}else{
	$val="";
}
return $val;
}

function getOldDecision($id_interv) {
	$sql="select decision from data_temp where id_interv='$id_interv' ";
	$res = mysql_query($sql);
	$r = mysql_fetch_object($res);
	return $r->decision;
}
















